package com.cn.hnust.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.sun.jmx.snmp.Timestamp;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.cn.hnust.pojo.Message;
import com.cn.hnust.pojo.Scenic;
import com.cn.hnust.service.IMessageService;
import com.cn.hnust.service.IScenicService;
import com.cn.hnust.util.Page;
import com.cn.hnust.util.Qiniu;

@Controller
@RequestMapping("/scenic")
public class ScenicController {
	public String uploadFileName;
	@Autowired
	private IScenicService scenicService;
	
	@Autowired
	private IMessageService messageservice;

	@RequestMapping(value = "/showScenic")
	public String toIndex(Model model,HttpSession session,HttpServletRequest request) throws Exception {
		List<Scenic> scenic = scenicService.getUsers();
		model.addAttribute("scenic", scenic);
		if(session.getAttribute("UserId")!=null){
			return "scenicList";
		}
		return "login";
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/scenicxg")
	public String scenicAdd(HttpServletRequest request, Model model, @RequestParam("pic") MultipartFile pic,
			int scenic_id) throws IOException {
		String scenic_name = request.getParameter("scenic_name");
		String scenic_dq = request.getParameter("scenic_dq");
		String scenic_jj = request.getParameter("scenic_jj");
		String scenic_jg = request.getParameter("scenic_jg");
		System.out.println(pic);
		int pageNow1 = 1;
		if (!StringUtils.isEmpty(pic)) {
			String path = "c:\\image\\";
			String originalFileName = pic.getOriginalFilename();
			// 新的图片名称
			String newFileName = UUID.randomUUID() + originalFileName.substring(originalFileName.lastIndexOf("."));
			// 新的图片
			String name = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(new Date());
			System.out.println(newFileName);
			String extension = FilenameUtils.getExtension(originalFileName);
			File newFile = new File(path + newFileName);
			String updz = path + newFileName;
			// 将内存中的数据写入磁盘

			pic.transferTo(newFile);

			String pho = null;

			Qiniu qiniuUtils = new Qiniu();
			pho = qiniuUtils.upload(newFileName, updz);
			Scenic sceni = new Scenic();
			sceni.setScenic_id(scenic_id);
			sceni.setScenic_dq(scenic_dq);
			sceni.setScenic_jj(scenic_jj);
			sceni.setScenic_name(scenic_name);
			sceni.setScenic_jg(scenic_jg);
			sceni.setScenic_tp(pho);
			
			scenicService.updateByPrimaryKey(sceni);
		
			Page page = null;// new 对象在下面
			int totalCount = scenicService.getCount();// 获取当前用户总记录条数

			page = new Page(totalCount, pageNow1);// 这样写的好处，判断完成外面可以继续调用
			List<Scenic> scenic = scenicService.queryAllRecord(page);
			model.addAttribute("page", page);
			model.addAttribute("scenic", scenic);
			
			return "scenicList";
		}
		return null;
	}

	/**
	 * 增加
	 */
	@RequestMapping(value = "/sceniczj")
	public String Sceniczj(HttpServletRequest request, Model model, @RequestParam("pic") MultipartFile pic) {

		String scenic_name = request.getParameter("scenic_name");
		String scenic_dq = request.getParameter("scenic_dq");
		String scenic_jj = request.getParameter("scenic_jj");
		String scenic_jg = request.getParameter("scenic_jg");
		int pageNow1 = 1;

		String path = "c:\\image\\";
		String originalFileName = pic.getOriginalFilename();
		// 新的图片名称
		String newFileName = UUID.randomUUID() + originalFileName.substring(originalFileName.lastIndexOf("."));
		// 新的图片
		String name = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(new Date());
		System.out.println(newFileName);
		String extension = FilenameUtils.getExtension(originalFileName);
		File newFile = new File(path + newFileName);
		String updz = path + newFileName;
		// 将内存中的数据写入磁盘

		try {
			pic.transferTo(newFile);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String pho = null;

		Qiniu qiniuUtils = new Qiniu();
		try {
			pho = qiniuUtils.upload(newFileName, updz);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Scenic sceni = new Scenic();

		sceni.setScenic_dq(scenic_dq);
		sceni.setScenic_jj(scenic_jj);
		sceni.setScenic_name(scenic_name);
		sceni.setScenic_jg(scenic_jg);
		sceni.setScenic_tp(pho);
		scenicService.insert(sceni);
		Page page = null;// new 对象在下面
		int totalCount = scenicService.getCount();// 获取当前用户总记录条数
		page = new Page(totalCount, pageNow1);// 这样写的好处，判断完成外面可以继续调用
		model.addAttribute("page", page);
		List<Scenic> scenic = scenicService.queryAllRecord(page);
		model.addAttribute("scenic", scenic);
		return "scenicList";
	}

	@RequestMapping(value = "/scenicadd")
	public String scenicadd() {

		return "scenicAdd";
	}

	/**
	 * 
	 * 景点的删除
	 */
	@RequestMapping(value = "/delscenic")
	@ResponseBody
	public Map<String, Object> delscenic(int id, Model model) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		scenicService.delscenic(id);
		Page page = null;// new 对象在下面
		int pageNow1 = 1;
		int totalCount = scenicService.getCount();// 获取当前用户总记录条数
		page = new Page(totalCount, pageNow1);// 这样写的好处，判断完成外面可以继续调用
		List<Scenic> scenic = scenicService.queryAllRecord(page);
		model.addAttribute("page", page);
		model.addAttribute("scenic", scenic);
		resultMap.put("result", "删除成功");
		return resultMap;
	}

	@RequestMapping(value = "/scenicmh")
	public String scenicmh(Model model, HttpServletRequest request, String pageNow) {
		Page page = null;// new 对象在下面
		String scenics = request.getParameter("scenic_name").trim();
		String scenic_name = "%" + scenics + "%";
		int totalCount = scenicService.getCountmh(scenic_name);// 获取当前用户总记录条数
		if (pageNow != null && pageNow != "") {// 防止出现空指针异常
			int pageNow1 = Integer.parseInt(pageNow);
			page = new Page(totalCount, pageNow1);// 这样写的好处，判断完成外面可以继续调用
		}
		
		
		List<Scenic> scenic = scenicService.selectPersons(scenic_name, page);
		model.addAttribute("page", page);
		model.addAttribute("scenic", scenic);
		
		
		return "scenicList";

	}

	@RequestMapping(value = "/queryAll")
	public String record(Model model, String pageNow,HttpSession session) {

		Page page = null;// new 对象在下面

		int totalCount = scenicService.getCount();// 获取当前用户总记录条数
		if (pageNow != null && pageNow != "") {// 防止出现空指针异常
			int pageNow1 = Integer.parseInt(pageNow);
			page = new Page(totalCount, pageNow1);// 这样写的好处，判断完成外面可以继续调用
		}
		List<Scenic> scenic = scenicService.queryAllRecord(page);// 取得当前用户的所有操作记录
		model.addAttribute("page", page);
		model.addAttribute("scenic", scenic);
		
			return "scenicList";
	
	}

	@RequestMapping(value = "/Updatejq")
	public String update(Model model, int id) {

		Scenic scenic = scenicService.getUser(id);

		model.addAttribute("scenic", scenic);

		return "scenicupdate";

	}

	@RequestMapping(value = "/qtindex")
	public String jqrecord(Model model) {
		
		List<Scenic> scenic = scenicService.queryReception();// 取得当前用户的所有操作记录
		
		model.addAttribute("scenic", scenic);

		return "/reception/index";
	}
	
	@RequestMapping(value = "/jqpurshase")
	public String jqpurshase(Model model,int scenic_id) {
		
		Scenic scenic = scenicService.getUser(scenic_id);// 取得当前用户的所有操作记录
		
		model.addAttribute("scenic", scenic);
		
		

		return "/reception/purchase";
	}
	
	@RequestMapping(value = "/jqzf")
	public String jqzf() {
		
		

		return "/reception/demo";
	}
	
	@RequestMapping(value = "/gaode")
	public String gaode() {
		
		

		return "/reception/degao";
	}
	
	

}
